import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class App {
    public static void main(String[] args) throws SQLException {
        //1.得到DataSource
        MysqlDataSource dataSource=new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/java33?character=utf8&useSSL=true");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        //2.得到Connection
        Connection connection=(Connection)dataSource.getConnection();
        //3.拼接sql，并执行
        String sql=" select * from student where id=?";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setInt(1,2);
        //4.执行查询
        ResultSet resultSet=statement.executeQuery();
        //statement.executeUpdate();//增加，删除，修改
        if(resultSet.next()){
            //有数据
            System.out.println("用户名:"+resultSet.getString("username"));
            System.out.println("email:"+resultSet.getString("mail"));
        }
        //关闭资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
